package com.chilly.io;

import org.junit.jupiter.api.Test;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Created by Chilly Cui on 2020/9/24.
 */
public class CharDemo {


    /**
     * 如果不执行close()或者flush()方法，数据只是保存到缓冲区，不会保存到文件。
     * 这点和与FileOutputStream不同，原因见 字节流和字符流的共同点章节
     */
    @Test
    void test3() throws IOException {
        FileWriter fileWriter = new FileWriter("c:/env/三国/孙权.txt");
        fileWriter.write(97);
        fileWriter.write('b');
        fileWriter.write('C');
        fileWriter.write("权");
        fileWriter.append("力");

        fileWriter.flush();
    }

    @Test
    void test2() throws IOException {
        FileReader fileReader = new FileReader("c:/env/三国/赵云.txt");
        int len;
        char[] data = new char[2];
        while ((len = fileReader.read(data)) != -1) {
            System.out.println(new String(data, 0, len));
        }
        //两个字符两个字符依次读取
    }


    @Test
    void test1() throws IOException {
        FileReader fileReader = new FileReader("c:/env//三国/赵云.txt");
        int b;
        while ((b = fileReader.read()) != -1) {
            System.out.println((char) b);
        }
    }
}
